﻿namespace HZY.Core.ApiResultManage;

/// <summary>
/// Api 消息返回类
/// </summary>
public class R : R<object>
{
    public R() : base()
    {
    }

    public R(int code, string? message, object? data) : base(code, message, data)
    {
    }

    /// <summary>
    /// 未登录或登录过期
    /// </summary>
    /// <returns></returns>
    public static R Unauthorized => ResultMessage(HttpStatusCode.Unauthorized, "未登录或登录过期!");

    /// <summary>
    /// 未授权
    /// </summary>
    public static R Forbidden => ResultMessage(HttpStatusCode.Forbidden, "未授权,请授权后访问!");


    #region result

    /// <summary>
    /// 返回消息
    /// </summary>
    /// <param name="code"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ResultMessage(HttpStatusCode code, string message)
        => new R((int)code, message, null);

    /// <summary>
    /// 返回数据
    /// </summary>
    /// <param name="code"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R ResultData(HttpStatusCode code, object? data)
        => new R((int)code, null, data);

    /// <summary>
    /// 可返回消息和数据
    /// </summary>
    /// <param name="code"></param>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Result(HttpStatusCode code, string message, object? data)
        => new R((int)code, message, data);

    #endregion

    #region result code 可传入 int

    /// <summary>
    /// 返回消息
    /// </summary>
    /// <param name="code"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ResultMessage(int code, string message)
        => new R(code, message, null);

    /// <summary>
    /// 返回数据
    /// </summary>
    /// <param name="code"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R ResultData(int code, object? data)
        => new R(code, null, data);

    /// <summary>
    /// 可返回消息和数据
    /// </summary>
    /// <param name="code"></param>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Result(int code, string message, object? data)
        => new R(code, message, data);

    #endregion

    #region Ok

    /// <summary>
    /// 成功 可返回消息
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R OkMessage(string message)
        => ResultMessage(HttpStatusCode.OK, message);

    /// <summary>
    /// 成功 可返回数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R OkData(object data)
        => ResultData(HttpStatusCode.OK, data);

    /// <summary>
    /// 成功 可返回 消息和数据
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Ok(string message, object? data)
        => Result(HttpStatusCode.OK, message, data);

    #endregion

    #region Error

    /// <summary>
    /// Error 可返回消息
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ErrorMessage(string message)
        => ResultMessage(HttpStatusCode.InternalServerError, message);

    /// <summary>
    /// Error 可返回数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R ErrorData(object? data)
        => ResultData(HttpStatusCode.InternalServerError, data);

    /// <summary>
    /// Error 可返回 消息和数据
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Error(string message, object? data)
        => Result(HttpStatusCode.InternalServerError, message, data);

    #endregion
}

/// <summary>
/// Api 消息返回类
/// </summary>
public class R<T>
{
    public R()
    {
        this.Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
    }

    public R(int code, string? message, T? data)
    {
        this.Code = code;
        this.Message = message;
        this.Data = data;
        this.Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
    }

    /// <summary>
    /// 编码
    /// </summary>
    public int Code { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 数据
    /// </summary>
    public T? Data { get; set; }

    /// <summary>
    /// 时间戳
    /// </summary>
    public string Timestamp { get; set; }
}